package sourceagile.webservices.git;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jettison.json.JSONArray;

import sourceagile.server.gitFileRepository.ListGitFiles;
import sourceagile.webservices.GetCallback;

@SuppressWarnings("serial")
public class ListGitFilesServlet extends HttpServlet {

	// http://localhost:8080/LiveSourceWebServices/ListGitFilesService
	// ?projectName=Jsonpfy
	// &projectURL=https://github.com/allineo/Jsonpfy.git
	// &username=allineo
	// &password=luizinho0820

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String projectName = request.getParameter("projectName");

		String projectURL = request.getParameter("projectURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		JSONArray list = ListGitFiles.listEntries(projectURL, projectName,
				username, password);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), list.toString());

		response.setContentType("text/javascript");

		response.getWriter().println(answer);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String projectName = request.getParameter("projectName");

		String projectURL = request.getParameter("projectURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		JSONArray list = ListGitFiles.listEntries(projectURL, projectName,
				username, password);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), list.toString());

		response.setContentType("text/javascript");

		response.getWriter().println(answer);
	}
}
